*****************************************************************
*This file builds vars on schooling outcomes from the Census/ACS*
*****************************************************************

use "$list/czones_list.dta", clear
expand 3
gen year=1990
bys czone: replace year=2000 if _n==2
bys czone: replace year=2008 if _n==3

merge m:1 czone using "$clean_data_lmarket/czone1990_schooling.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2000_schooling.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2008_schooling.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2016_schooling.dta", assert(3) nogenerate

merge m:1 czone using "$clean_data_lmarket/czone1980_schooling.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone1970_schooling.dta", assert(3) nogenerate


foreach var of varlist ipums_* {
quiet replace `var' = 0 if mi(`var')
}

*Schooling
foreach school in sc {

gen d_`school' = 0.7*100*((ipums_pop_`school'_2000/ipums_pop_2000)-(ipums_pop_`school'_1990/ipums_pop_1990)) if year == 1990
replace d_`school' = 100*((ipums_pop_`school'_2008/ipums_pop_2008)-(ipums_pop_`school'_2000/ipums_pop_2000)) if year == 2000
replace d_`school' = 100*((ipums_pop_`school'_2016/ipums_pop_2016)-(ipums_pop_`school'_2008/ipums_pop_2008)) if year == 2008

*Pre-trends (controls)
gen d_`school'_70_90 = 100*((ipums_pop_`school'_1990/ipums_pop_1990)-(ipums_pop_`school'_1970/ipums_pop_1970))

*Pre-trends (outcome)
gen d_`school'_70_80_90 = 100*((ipums_pop_`school'_1980/ipums_pop_1980)-(ipums_pop_`school'_1970/ipums_pop_1970)) if year == 1990
replace d_`school'_70_80_90 = 100*((ipums_pop_`school'_1990/ipums_pop_1990)-(ipums_pop_`school'_1980/ipums_pop_1980)) if year == 2000

*Age
foreach age in a19_24 a25_29 a30_34 a35_39 a40_44 a45_49 a50_54 a55_59 a60_64 {

gen d_`school'_`age' = 0.7*100*((ipums_pop_`school'_`age'_2000/ipums_pop_`age'_2000)-(ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990)) if year == 1990
replace d_`school'_`age' = 100*((ipums_pop_`school'_`age'_2008/ipums_pop_`age'_2008)-(ipums_pop_`school'_`age'_2000/ipums_pop_`age'_2000)) if year == 2000
replace d_`school'_`age' = 100*((ipums_pop_`school'_`age'_2016/ipums_pop_`age'_2016)-(ipums_pop_`school'_`age'_2008/ipums_pop_`age'_2008)) if year == 2008

}
}

*Changes in population
gen dln_pop = 0.7*100*(ln(ipums_pop_2000)-ln(ipums_pop_1990)) if year == 1990
replace dln_pop = 100*(ln(ipums_pop_2008)-ln(ipums_pop_2000)) if year == 2000
replace dln_pop = 100*(ln(ipums_pop_2016)-ln(ipums_pop_2008)) if year == 2008


save "$final_data_outcomes/czone_Students.dta", replace






